package org.panda.springsecurity6_x.user;

import org.panda.springsecurity6_x.entity.SysUser;
import org.panda.springsecurity6_x.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @param
 * @Description:
 * @author：FanZhongyu
 * @date 2024/4/9 15:32
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getByUserName(username);
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }


        return new AccountUser(sysUser.getUserId(), sysUser.getUserName(), sysUser.getPassword(), null);
    }
}
